--- layout: post title: "Real Time Facial Recognition in Python" subtitle: "Adding filters to live video data" date: 2022-08-17 5:19:00 -0400 tags: jupyter_notebook fun classification video_data background: '/img/posts/bgp6.jpg' ---
This is a quick post showcasing how to write a real-time face/eye tracking routine in Python using OpenCV. I'll show how this routine can be used to add fun filters :]
Here are the most important modules used for this project:
The API documentation for each of these modules can be found here:
#Import modules
import cv2
def objectTracker1(cascPath):
faceCascade = cv2.CascadeClassifier(cascPath)
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
while True: # try to get the first frame
rval, frame = vc.read()
# Capture frame-by-frame
ret, frame = vc.read()
#Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
objects = faceCascade.detectMultiScale(gray, scaleFactor=1.2,
minNeighbors=1, minSize=(40, 40),
flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in objects:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
vc.release()
cv2.destroyWindow("preview")
In my previous post where I determined player jersey colors from video footage, I briefly explored using the HOG package to detect human figures where I had initial success. Upon looking through the various classifiers in OpenCVs documentation, I came across the 'Haar Cascades' libraries which extend the base HOG descriptors to include things like cats and different facial profiles.
After playing with the cascade parameters a bit, I got the result below
face_cascade_Path = r'haarcascades\haarcascade_frontalface_alt.xml'
objectTracker1(face_cascade_Path)